package com.hymjweb.demo.hymj.master_bill_manage.pull.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.master_bill_manage.pull.bean.BMasterBillPullPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.hymjweb.demo.hymj.master_bill_manage.pull.mapper
 * @Author: luoge
 * @CreateTime: 2020-12-30 16:42
 * @Description:
 */

@Mapper
@Component
public interface BMasterBillPullMapper {


    class BMasterBillPullMapperProvider{

        public String findAll(Map param){
            String sql= new SQL() {{
                SELECT("t.* ");
                FROM("b_master_bill_pull t ");
                WHERE("1 = 1");
            }}.toString();

            // 主单号
            String keyMasterBillCode = DataTypeUtil.toStringObject(param.get("keyMasterBillCode"));

            // 主单类型
            Long keyMasterBillTypeId = DataTypeUtil.toLongObject(param.get("keyMasterBillTypeId"));

            // 日期
            String keyPullStartDate = DataTypeUtil.toStringObject(param.get("keyPullStartDate"));
            String keyPullEndDate = DataTypeUtil.toStringObject(param.get("keyPullEndDate"));

            // 获取操作员与操作部门id
            String czyId = DataTypeUtil.toStringObject(param.get("czyId"));
            String departmentId = DataTypeUtil.toStringObject(param.get("departmentId"));

            if(keyMasterBillCode != null){
                sql += " and t.master_bill_id = " + DataTypeUtil.quoteStr(keyMasterBillCode);
            }

            if(keyMasterBillTypeId != null){
                sql += " and t.master_bill_type_id = " + keyMasterBillTypeId;
            }

            if(keyPullStartDate!=null && !"".equals(keyPullStartDate)){
                if(keyPullEndDate!=null && !"".equals(keyPullEndDate)){
                    int d=keyPullStartDate.compareTo(keyPullEndDate);
                    if(d>0){
                        String tempDate = keyPullStartDate;
                        keyPullStartDate = keyPullEndDate;
                        keyPullEndDate = tempDate;
                    }
                    // and (date_format(t.create_time,'%Y-%m-%d') between keyOpenBillStartDate and keyOpenBillEndDate)
                    sql += " and (date_format(t.create_time,'%Y-%m-%d') between "+ DataTypeUtil.quoteStr(keyPullStartDate) +" and " + DataTypeUtil.quoteStr(keyPullEndDate) + ")";
                }
                else{
                    sql += " and date_format(t.create_time,'%Y-%m-%d') >= "+ DataTypeUtil.quoteStr(keyPullStartDate);
                }
            }
            else{
                if(keyPullEndDate!=null && !"".equals(keyPullEndDate)){
                    sql += " and date_format(t.create_time,'%Y-%m-%d') <= "+ DataTypeUtil.quoteStr(keyPullEndDate);
                }
            }


            return sql;
        }

        public String insert(BMasterBillPullPojo bMasterBillPullPojo) {
            String sql = new SQL() {{
                INSERT_INTO("b_master_bill_pull");
                VALUES("id","#{id}");
                VALUES("master_bill_type_id","#{masterBillTypeId}");
                VALUES("master_bill_id","#{masterBillId}");

                VALUES("pull_dispose_number","#{pullDisposeNumber}");
                VALUES("pull_dispose_actual_weight","#{pullDisposeActualWeight}");
                VALUES("pull_dispose_chargeable_weight","#{pullDisposeChargeableWeight}");
                VALUES("pull_dispose_volume","#{pullDisposeVolume}");

                VALUES("pull_dispose_transport_cost_amount","#{pullDisposeTransportCostAmount}");
                VALUES("pull_dispose_ground_amount","#{pullDisposeGroundAmount}");
                VALUES("pull_dispose_airlines_add_cost_amount","#{pullDisposeAirlinesAddCostAmount}");
                VALUES("pull_dispose_other_charges_amount","#{pullDisposeOtherChargesAmount}");

                VALUES("pull_dispose_amount","#{pullDisposeAmount}");
                VALUES("pull_dispose_comment","#{pullDisposeComment}");

                VALUES("create_time", "#{createTime}");
                VALUES("create_user_id", "#{createUserId}");
                VALUES("update_time", "#{updateTime}");
                VALUES("update_user_id", "#{updateUserId}");
            }}.toString();
            return sql;
        }

        public String update(BMasterBillPullPojo bMasterBillPullPojo) {
            String sql= new SQL() {{
                UPDATE("b_master_bill_pull t");
                SET("t.master_bill_type_id = #{masterBillTypeId}");
                SET("t.master_bill_id = #{masterBillId}");

                SET("t.pull_dispose_number = #{pullDisposeNumber}");
                SET("t.pull_dispose_actual_weight = #{pullDisposeActualWeight}");
                SET("t.pull_dispose_chargeable_weight = #{pullDisposeChargeableWeight}");
                SET("t.pull_dispose_volume = #{pullDisposeVolume}");

                SET("t.pull_dispose_transport_cost_amount = #{pullDisposeTransportCostAmount}");
                SET("t.pull_dispose_ground_amount = #{pullDisposeGroundAmount}");
                SET("t.pull_dispose_airlines_add_cost_amount = #{pullDisposeAirlinesAddCostAmount}");
                SET("t.pull_dispose_other_charges_amount = #{pullDisposeOtherChargesAmount}");

                SET("t.pull_dispose_amount = #{pullDisposeAmount}");
                SET("t.pull_dispose_comment = #{pullDisposeComment}");

                SET("t.update_time = #{updateTime}");
                SET("t.update_user_id = #{updateUserId}");

                WHERE("t.id = #{id}");
            }}.toString();
            return sql;
        }

        public String delete(String id){
            String sql = new SQL() {{
                DELETE_FROM("b_master_bill_pull t");
                WHERE("t.id = #{id}");
            }}.toString();
            return sql;
        }

        public String deleteByMasterBillId(String masterBillId){
            String sql = new SQL() {{
                DELETE_FROM("b_master_bill_pull t");
                WHERE("t.master_bill_id = #{masterBillId}");
            }}.toString();
            return sql;
        }


        public String getBMasterBillPullPojoById(String id){
            String sql= new SQL() {{
                SELECT("t.* ");
                FROM("b_master_bill_pull t ");
                WHERE("t.id = " + DataTypeUtil.quoteStr(id));
            }}.toString();
            return sql;
        }


        public String getBMasterBillPullPojoByMasterBillIdAndBillTypeId(String masterBillId, Long billTypeId){
            String sql= new SQL() {{
                SELECT("t.* ");
                FROM("b_master_bill_pull t ");
                WHERE("t.master_bill_type_id = #{billTypeId} and t.master_bill_id = #{masterBillId}");
            }}.toString();
            return sql;
        }

    }


    @Results(id = "bMasterBillPullPojoByIdMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "master_bill_type_id", property = "masterBillTypeId"),
            @Result(column = "master_bill_id", property = "masterBillId"),

            @Result(column = "pull_dispose_number", property = "pullDisposeNumber"),
            @Result(column = "pull_dispose_actual_weight", property = "pullDisposeActualWeight"),
            @Result(column = "pull_dispose_chargeable_weight", property = "pullDisposeChargeableWeight"),
            @Result(column = "pull_dispose_volume", property = "pullDisposeVolume"),

            @Result(column = "pull_dispose_transport_cost_amount", property = "pullDisposeTransportCostAmount"),
            @Result(column = "pull_dispose_ground_amount", property = "pullDisposeGroundAmount"),
            @Result(column = "pull_dispose_airlines_add_cost_amount", property = "pullDisposeAirlinesAddCostAmount"),
            @Result(column = "pull_dispose_other_charges_amount", property = "pullDisposeOtherChargesAmount"),

            @Result(column = "pull_dispose_amount", property = "pullDisposeAmount"),
            @Result(column = "pull_dispose_comment", property = "pullDisposeComment"),

            @Result(column = "create_time", property = "createTime"),
            @Result(column = "create_user_id", property = "createUserId"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "update_user_id", property = "updateUserId")
    })
    @SelectProvider(type = BMasterBillPullMapperProvider.class, method = "getBMasterBillPullPojoById")
    BMasterBillPullPojo getBMasterBillPullPojoById(String id);


    @ResultMap("bMasterBillPullPojoByIdMapId")
    @SelectProvider(type = BMasterBillPullMapperProvider.class, method = "findAll")
    List<BMasterBillPullPojo> findAll(Map param);


    @ResultMap("bMasterBillPullPojoByIdMapId")
    @SelectProvider(type = BMasterBillPullMapperProvider.class, method = "getBMasterBillPullPojoByMasterBillIdAndBillTypeId")
    BMasterBillPullPojo getBMasterBillPullPojoByMasterBillIdAndBillTypeId(String masterBillId, Long billTypeId);


    @InsertProvider(type = BMasterBillPullMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(BMasterBillPullPojo bMasterBillPullPojo);

    @UpdateProvider(type = BMasterBillPullMapperProvider.class, method = "update")
    int update(BMasterBillPullPojo bMasterBillPullPojo);

    @DeleteProvider(type = BMasterBillPullMapperProvider.class, method = "delete")
    int delete(String id);


    @DeleteProvider(type = BMasterBillPullMapperProvider.class, method = "deleteByMasterBillId")
    int deleteByMasterBillId(String masterBillId);


}
